package com.sky.controller.admin;

import com.sky.result.Result;
import com.sky.utils.AliOssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;


@RestController
@RequestMapping("/admin/common")
@Slf4j
@CrossOrigin
public class CommonController {

    @Autowired
    private AliOssUtil aliOssUtil;



    @PostMapping("/upload")
    public Result<String> upload(@RequestBody MultipartFile file) {


        String url = null;
        try {
            String originalName = file.getOriginalFilename();
            String[] split = originalName.split("\\.");
            String newName = UUID.randomUUID() + "."+split[1];
            log.info("文件上传：{}",originalName);

            url = aliOssUtil.upload(file.getBytes(), newName);
        } catch (IOException e) {
            log.info("文件上传失败:{}",e);
            return Result.error("文件上传失败");
        }

        return Result.success(url);
    }
}
